<?php
    //门店号,版本号等信息
	const BUSI_CODE         = "";
	const API_VERSION       = "v46";
	const GF_APPID          = "";
	const GF_APP_SECRET     = "";
	const GF_API_URL_PREFIX = "http://open.guangfen.net/api/";

	//支持的就交易渠道
	const PAY_CHANNEL_ALIPAY      = "ALIPAY";
	const PAY_CHANNEL_WECHAT      = "WECHAT";
	const PAY_CHANNEL_CLOUD_UNION = "CLOUD_UNION";

	//获取签名
	function getSign($array) {
	    ksort($array);
		$sign_data = "";
		foreach($array as $key => $value){
		//echo "{$key}==>{$value}<br>";
			if ("{$value}" != "") {
				$sign_data .= "{$key}={$value}&";
			}
		}

		$sign_data .= "APP_SECRET=".GF_APP_SECRET;

		echo "待签名字符串:".$sign_data."<br>";

		$sign = strtoupper(md5($sign_data));
		echo "签名:".$sign."<br>";

		return $sign;
	}

	//获取ip
	function get_local_ip() {
		$preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
		//获取操作系统为win2000/xp、win7的本机IP真实地址
		exec("ipconfig", $out, $stats);
		if (!empty($out)) {
			foreach ($out AS $row) {
				if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv4")) {
					$tmpIp = explode(":", $row);
					if (preg_match($preg, trim($tmpIp[1]))) {
						return trim($tmpIp[1]);
					}
				}
			}
		}
		//获取操作系统为linux类型的本机IP真实地址
		exec("ifconfig", $out, $stats);
		if (!empty($out)) {
			if (isset($out[1]) && strstr($out[1], 'addr:')) {
				$tmpArray = explode(":", $out[1]);
				$tmpIp = explode("", $tmpArray[1]);
				if (preg_match($preg, trim($tmpIp[0]))) {
					return trim($tmpIp[0]);
				}
			}
		}
		return '127.0.0.1';
	}

	//执行curl操作
	function exec_curl($arr, $post_url) {
		$post_data = $arr;

		// create a new cURL resource
		$ch = curl_init();

		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $post_url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false ); // 不验证证书
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false ); // 不验证HOST
		curl_setopt($ch, CURLOPT_SSLVERSION, 1 ); // http://php.net/manual/en/function.curl-setopt.php页面搜CURL_SSLVERSION_TLSv1

		//设置post数据
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		// grab URL and pass it to the browser
		$result_arr = curl_exec($ch);

		if(curl_errno($ch)){
			$errmsg = curl_error($ch);
			curl_close($ch);
			echo("请求失败,报错信息>".$errmsg );
			return null;
		}
		if( curl_getinfo($ch, CURLINFO_HTTP_CODE) != "200"){
			$errmsg = "http状态=" . curl_getinfo($ch, CURLINFO_HTTP_CODE);
			curl_close($ch);
			echo("请求失败,报错信息>".$errmsg );
			return null;
		}

		// close cURL resource, and free up system resources
		curl_close($ch);

		return $result_arr;
	}
?>